package com.example.user.customviewdemo.slideletelistview.scroll;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import com.example.user.customviewdemo.R;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ScrollActivity extends AppCompatActivity {


    @Bind(R.id.scroll_to)
    Button scrollTo;
    @Bind(R.id.scroll_by)
    Button scrollBy;
    @Bind(R.id.scroll_reset)
    Button scrollReset;
    @Bind(R.id.scroll_layout)
    LinearLayout scrollLayout;

    private float preX;
    private float startX;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scroll);
        ButterKnife.bind(this);

        scrollLayout.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        preX = event.getX();
                        startX = event.getX();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        //使用scrollTo（）
                        float x = event.getX();
                        int dx = (int) (preX - x) + scrollLayout.getScrollX(); //移动距离 + 现在的scrollX
                        if(dx >= 300){
                            dx = 300;
                        }else if(dx < 0){
                            dx = 0;
                        }
                        scrollLayout.scrollTo(dx,0);
                        preX = x;
                        break;
                        //使用scrollBy（）存在bug，超过之后会出现晃动
                       /* float scrollX = scrollLayout.getScrollX();
                        if(scrollX > 200){
                            scrollLayout.scrollTo(200,0);
                            return true;
                        }else if(scrollX < 0){
                            scrollLayout.scrollTo(0,0);
                            return true;
                        }
                       float x = event.getX();
                        int dx = (int) (preX - x);
                        scrollLayout.scrollBy(dx,0);
                        preX = x;
                        break;*/
                    case MotionEvent.ACTION_UP:
                        float upX = event.getX();
                        int dx1 = (int) (startX - upX);
                        int absDx1 = Math.abs(dx1);
                        if(dx1>0 && absDx1 >= 60){
                            scrollLayout.scrollTo(300,0);
                        }else if(dx1 < 0 && absDx1 >=60){
                            scrollLayout.scrollTo(0,0);
                        }
                        break;

                }
                return true;
            }
        });


    }

    @OnClick({R.id.scroll_to, R.id.scroll_by, R.id.scroll_reset})
    public void onClickListener(View v) {
        switch (v.getId()) {
            case R.id.scroll_to:
                scrollLayout.scrollTo(2, 2);
                break;
            case R.id.scroll_by:
                scrollLayout.scrollBy(50, 0);
                break;
            case R.id.scroll_reset:
                scrollLayout.scrollTo(0, 0);
                break;
        }
    }
}
